package com.cbg.config;


import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * 配置多数据源
 *
 * @author wangjuzheng
 * @date 2019-11-21
 **/
@Configuration
public class DataSourceConfig {

    /***
     * 配置主数据源
     * @return 主数据源
     */
    @Primary
    @Bean("symDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.sym")
    public DataSource symDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    /****
     * 配置标签数据源
     * @return 标签数据源
     */
    @Bean("labelDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.label")
    public DataSource labelDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "symJdbcTemplate")
    public JdbcTemplate symJdbcTemplate(@Qualifier("symDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "labelJdbcTemplate")
    public JdbcTemplate labelJdbcTemplate(@Qualifier("labelDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
